package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.FunctionAutoBrightness;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 设置多功能卡的自动亮度
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetFunctionAutoBrightness implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 多功能卡自动亮度数据
         */
        private List<FunctionAutoBrightness> autoBrightness;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 多功能卡自动亮度数据
         */
        private List<FunctionAutoBrightness> autoBrightness;
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetFunctionAutoBrightness.InputMsg.Builder inputMsg =
                CompleteInstruction.SetFunctionAutoBrightness.InputMsg.newBuilder();
        SetFunctionAutoBrightness.InputMsg thisInput = this.getInput();
        inputMsg.addAllAutoBrightness(thisInput.getAutoBrightness().stream().map(FunctionAutoBrightness::toProto).collect(Collectors.toList()));
        final CompleteInstruction.SetFunctionAutoBrightness.Builder builder =
                CompleteInstruction.SetFunctionAutoBrightness.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public SetFunctionAutoBrightness byteConvertObject(ByteString bytes) {
        CompleteInstruction.SetFunctionAutoBrightness setFunctionAutoBrightness = null;
        try {
            setFunctionAutoBrightness = CompleteInstruction.SetFunctionAutoBrightness.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(setFunctionAutoBrightness)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SetFunctionAutoBrightness.OutputMsg outputMsg = new SetFunctionAutoBrightness.OutputMsg();
        CompleteInstruction.SetFunctionAutoBrightness.OutputMsg output = setFunctionAutoBrightness.getOutput();
        List<FunctionAutoBrightness> autoBrightness = output.getAutoBrightnessList().stream()
                .map(proto -> new FunctionAutoBrightness().formProto(proto))
                .collect(Collectors.toList());
        outputMsg.setAutoBrightness(autoBrightness);
        this.setOutput(outputMsg);
        return this;
    }
}

